package com.xxxx.seckill.rabbitmq;

import com.xxxx.seckill.VO.GoodsVO;
import com.xxxx.seckill.VO.ResponseBean;
import com.xxxx.seckill.VO.ResponseBeanEnum;
import com.xxxx.seckill.pojo.SeckillMessage;
import com.xxxx.seckill.pojo.SeckillOrder;
import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.IGoodsService;
import com.xxxx.seckill.service.IOrderService;
import com.xxxx.seckill.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 消息接收者（消费者）
 */
@Service
@Slf4j
public class MQReceiver {

    @RabbitListener(queues = "queue")
    public void receive(Object message) {
        log.info("接收消息：{}", message);
    }

    @RabbitListener(queues = "queue_fanout01")
    public void receiveFanout01(Object message) {
        log.info("QUEUE01接收消息：{}", message);
    }

    @RabbitListener(queues = "queue_fanout02")
    public void receiveFanout02(Object message) {
        log.info("QUEUE02接收消息：{}", message);
    }

    @RabbitListener(queues = "queue_direct01")
    public void receiveDirect01(Object message) {
        log.info("QUEUE01接收消息：{}", message);
    }

    @RabbitListener(queues = "queue_direct02")
    public void receiveDirect02(Object message) {
        log.info("QUEUE02接收消息：{}", message);
    }

    @RabbitListener(queues = "queue_topic01")
    public void receiveTopic01(Object message) {
        log.info("QUEUE01接收消息：{}", message);
    }

    @RabbitListener(queues = "queue_topic02")
    public void receiveTopic02(Object message) {
        log.info("QUEUE02接收消息：{}", message);
    }

    @RabbitListener(queues = "queue_headers01")
    public void receiveHeaders01(Message message) {
        log.info("QUEUE01接收Message对象：{}", message);
        log.info("QUEUE01接收消息：{}", new String(message.getBody()));
    }

    @RabbitListener(queues = "queue_headers02")
    public void receiveHeaders02(Message message) {
        log.info("QUEUE02接收Message对象：{}", message);
        log.info("QUEUE02接收消息：{}", new String(message.getBody()));
    }

    // 分隔线
    // 分隔线
    // 分隔线
    // 分隔线
    // 分隔线

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private IOrderService orderService;

    /**
     * 下单操作
     *
     * @param message
     */
    @RabbitListener(queues = "seckillQueue")
    public void receiveSeckillMessage(String message) {
        log.info("接收信息：{}", message);
        SeckillMessage seckillMessage = JsonUtil.jsonStr2Object(message, SeckillMessage.class);
        Long goodsId = seckillMessage.getGoodsId();
        User user = seckillMessage.getUser();
        GoodsVO goodsVO = goodsService.findGoodsVOByGoodsId(goodsId);
        // 检查库存
        if (goodsVO.getSeckillStock() < 1) {
            return;
        }
        // 检查是否重复抢购
        SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("seckill_order:" + user.getId() + ":" + goodsId);
        if (seckillOrder != null) {
            return;
        }
        // 下单操作
        orderService.seckill(user, goodsVO);
    }
}
